﻿using System.Collections.Generic;
using System.Data;

namespace Marmat.Data
{
    public class ViewTableHelper
    {
        public string TableName { get; set; }
        public Suffix Suffix { get; set; }

        public List<ViewRowHelper.Column> PrimaryKey
        {
            get
            {
                if (this.m_PrimaryKey is null)
                {
                    this.m_PrimaryKey = new List<ViewRowHelper.Column>();
                }

                return this.m_PrimaryKey;
            }
        }

        private List<ViewRowHelper.Column> m_PrimaryKey;

        public string GetSaveString(DataTable dt, ViewRowHelper.Column[] Columns)
        {
            var Result = new System.Text.StringBuilder();
            var Helper = new ViewRowHelper();
            Helper.TableName = this.TableName;
            Helper.Suffix = new Suffix();
            Helper.PrimaryKey.AddRange(this.PrimaryKey.ToArray());
            foreach (DataRow dr in dt.Rows)
            {
                string Sql = Helper.GetSaveString(dt, dr, Columns);
                if (!string.IsNullOrEmpty(Sql))
                {
                    Result.Append((Result.Length > 0 ? "\r\n" : "") + Sql);
                }
            }

            return Result.ToString();
        }
    }
}